from tkinter import filedialog,messagebox,Tk,StringVar,Label,Entry,Button,Text
# 导入ttk模块，因为下拉菜单控件Combobox在ttk中
from tkinter.ttk import Combobox
from os.path import basename
from readTxt2ExcelGUI import start

class App:
    def __init__(self):
        self.keywords = ("口","啄","喙","噣","咮")  # 关键词
        self.main_box = Tk()
        self.main_box.title("文本标记工具") # 设置窗口名
        # self.main_box.geometry("400x100") # 设置窗口大小
        self.main_box.geometry(None)
        self.path = StringVar() # 定义路径变量
        self.fullpath = ""

        # 文件选择
        # 输入框文本，标记，按键
        self.label = Label(self.main_box,text = "目标路径：")
        
        # 输入框绑定变量path
        self.entry = Entry(self.main_box, textvariable = self.path,state="disabled",width=20)
        self.select_button = Button(self.main_box, text = "选择文件", command = self.selectPath,width=10)

        # 创建下拉列表，设置下拉列表中的值
        self.normal_ddl = Label(self.main_box, text='下拉框选项：')
        self.ddl = Combobox(self.main_box,width=17)
        self.ddl['value'] = self.keywords
        self.ddl.current(0)
        self.ddl.bind("<<ComboboxSelected>>",self.select)

        # 确认按钮
        self.confirm = Button(self.main_box,text ="确认",command = self.confirmCallBack,width=10)
        self.reset = Button(self.main_box,text ="重置",command = self.reset,width=10)

        # 底部信息
        self.author = Text(self.main_box, width=8,height=1,font=('楷体', 10,'italic bold'),bg="#f0f0f0",relief="flat")
        self.author.insert('0.0','@Railgun')
        self.author.config(state="disabled")

        '''grid布局'''
        # N/S/E/W，分别代表上/下/左/右
        # 选择文件
        self.label.grid(row = 0, column = 1, sticky="W",padx=10,pady=10)
        self.entry.grid(row = 0, column = 2,sticky="NW",padx=10,pady=10)
        self.select_button.grid(row = 0, column = 6,sticky="W",padx=10,pady=10)
        # 下拉框
        self.normal_ddl.grid(row=1, column=1, sticky='W',padx=10,pady=10)
        self.ddl.grid(row=1, column=2, sticky='NW',padx=10,pady=10)
        # 确认按钮
        self.confirm.grid(row=2, column=6, sticky='NW',padx=10,pady=10)
        # 重置按钮
        self.reset.grid(row=2, column=1,padx=10,pady=10)
        # 底部信息
        self.author.grid(row=3, column=2,padx=10,pady=10)


        self.main_box.mainloop()

    def selectPath(self):
        #选择文件path_接收文件地址
        path_ = filedialog.askopenfilename(filetypes=[('TXT', '*.txt'), ('All Files', '*')])
        #通过replace函数替换绝对文件地址中的/来使文件可被程序读取 
        #注意：\\转义后为\，所以\\\\转义后为\\
        path_=path_.replace("/","\\\\")
        self.fullpath = path_
        base_name = basename(path_)
        #path设置path_的值
        self.path.set(base_name)

    # 点击下拉框回调
    def select(self,_):
        print(self.ddl.get()) # 获取选中的关键词

    def confirmCallBack(self):
        if not self.path.get().strip(' '):
            messagebox.showwarning(title="警告", message="文件名不能为空")
            return

        # 执行标记操作
        start(self.fullpath,self.ddl.get())
        print("导出成功")

    def reset(self):
        self.ddl.current(0)
        self.path.set("")
        self.fullpath = ""

if __name__ == '__main__':
    App()
